package com.zmzncs.lmtc.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.zmzncs.lmtc.common.pojo.ResultHead;
import com.zmzncs.lmtc.common.pojo.daoer.MonthAmount;
import com.zmzncs.lmtc.common.pojo.daoer.ParkingInfo;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * 道尔
 */
@Slf4j
@Component
public class DaoerApiUtil {

    //测试环境
//    final static String REFERER = "https://cloud-beta.drzk.cn";

    //PRO环境
    final static String REFERER= "https://cloud-2018.drzk.cn";

    final static String SUCCESS_STATUS = "1";
    final static String TOKEN_URL = "/api/index/auth/token";
    final static String GET_CAR_FEE = "/api/index/tempFee/getcarfee";
    final static String ONANDOFF =  "/api/index/onandoff";
    final static String GET_CHANNELS =  "/api/index/getchannels";
//    final static String GET_CARFEE =  "/apiDocumentation?url=tempCar&name=0";
    final static String GET_CARFEE =  "/api/index/tempFee/getcarfee";
    final static String GET_PARKING =  "/api/getparking";
    final static String GET_MONTH_AMOUNT =  "/api/index/monthlycar/long-rental-rate";


    @Autowired
    ParkInfoDao parkInfoDao;

    private static DaoerApiUtil daoerApiUtil;
    @PostConstruct
    public void init() {
        daoerApiUtil = this;
    }



    /**
     * 获取token
     */
    public static synchronized  String getToken(String parkId,String appName) {
        MapCacheManager.deleteTimeOut();
        String token = (String)MapCacheManager.getCache("token");
        if(StringUtils.isBlank(token)){

            Map<String, Object> paramsa = Maps.newHashMap();
            paramsa.put("appName", appName);
            paramsa.put("parkId", parkId );//停车场ID
            String jsonParams =  JSON.toJSONString(paramsa);
            String response =  HttpUtil.getIntance().postJson(REFERER+TOKEN_URL, jsonParams);
            String status = JSONObject.parseObject(response).get("status").toString();
            if(SUCCESS_STATUS.equals(status)){
                token = JSONObject.parseObject(response).get("data").toString();
                MapCacheManager.setCache("token",token,7200*1000);
                return token;
            }
            log.error("获取道尔token错误：" + response);
        }
        return token;
    }


    /**
     * 获取停车场信息
     * @param parkId    第三方应用车场编号
     */
    public static ParkingInfo getParking(String parkId,String appName) {

        String token = getToken(parkId, appName);
        if(StringUtils.isBlank(token)){
            return null;
        }
        Map<String, String> headers = Maps.newHashMap();
        headers.put("token",token);
        headers.put("Referer",REFERER);

        String resultStr = HttpUtil.getIntance().get(REFERER+GET_PARKING+"/"+parkId,
                null, headers);
        log.info("请求响应结果：" + resultStr);
        ResultHead resultHead = JsonUtil.JSONToObj(resultStr, ResultHead.class);
        String status = resultHead.getHead().getStatus();
        if(SUCCESS_STATUS.equals(status)){
            ParkingInfo parkingInfo = JsonUtil.JSONToObj(resultStr, ParkingInfo.class);
            return parkingInfo;
        }
        return null;
    }

    /**
     * 获取月租费率
     * @param parkId    第三方应用车场编号
     */
    public static MonthAmount getMonthAmount(String parkId,String appName) {
        log.info("获取停车场信息：【{}】", parkId);
        String token = getToken(parkId,appName);
        if(StringUtils.isBlank(token)){
            return null;
        }
        Map<String, String> headers = Maps.newHashMap();
        headers.put("token",token);
        headers.put("Referer",REFERER);

        String resultStr = HttpUtil.getIntance().get(REFERER+GET_MONTH_AMOUNT+"/"+parkId,
                null, headers);
        log.info("请求响应结果：" + resultStr);
        ResultHead resultHead = JsonUtil.JSONToObj(resultStr, ResultHead.class);
        String status = resultHead.getHead().getStatus();

        if(SUCCESS_STATUS.equals(status)){
            MonthAmount monthAmount = JsonUtil.JSONToObj(resultStr, MonthAmount.class);
            return monthAmount;
        } else {
            ExceptionUtil.businessException("获取道尔停车场月租规则信息失败");
        }
        return null;
    }


}
